using System;
using System.Windows.Forms.VisualStyles;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.WIQL;

namespace TXLooker.WorkItemExtention.Core
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class OlWIQueryColumn
    {
        public static int DefaultColumnWidth = 200;
        public static HorizontalAlign DefaultTextAlign = HorizontalAlign.Left;

        /// <summary>
        /// Pattern for XmlNode in xml text for view
        /// </summary>
        private static string XmlPattern =
           @"<column>
                <heading>@caption</heading>
                <prop>http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/@property</prop>
                <type>string</type>                
                <width>@width</width>
                <style>padding-left:3px;text-align:@align</style>
            </column>";
        /// <summary>
        /// Create XML text for column node in XML Otlook Folder View
        /// </summary>
        /// <param name="field">WorkItem field definition (bind WorkItem field with column)</param>
        /// <param name="width">Column width</param>
        /// <param name="olItemPropertyName">Property name in Outlook Item (get via indexer 'Properties' in OlItem for data displaying)</param>
        /// <param name="textalign">Text align for data displaying</param>
        /// <returns></returns>
        public static string CreateOlXML( FieldDefinition field, int width, string olItemPropertyName, HorizontalAlign textalign )
        {
            if( width < 0 )
                throw new ArgumentException();

            switch(field.FieldType)
            {
                case FieldType.Integer:
                    return XmlPattern.Replace("@caption", field.Name).Replace("@property", olItemPropertyName).Replace("<type>string</type>", "<type>i4</type>").Replace("@width", width.ToString()).Replace("@align", textalign.ToString().ToLower());
                case FieldType.DateTime:
                    return XmlPattern.Replace("@caption", field.Name).Replace("@property", olItemPropertyName).Replace("<type>string</type>", "<type>datetime</type>").Replace("@width", width.ToString()).Replace("@align", textalign.ToString().ToLower());
                default:
                    return XmlPattern.Replace("@caption", field.Name).Replace("@property", olItemPropertyName).Replace("@width", width.ToString()).Replace("@align", textalign.ToString().ToLower());
                    
            }
        }
        public static string CreateOlXML( WorkItemFieldDefinition field, int width, string olItemPropertyName, HorizontalAlign textalign )
        {
            if( width < 0 )
                throw new ArgumentException();

            switch (field.FieldType)
            {
                case FieldType.Integer:
                    return XmlPattern.Replace("@caption", field.Name).Replace("@property", olItemPropertyName).Replace("<type>string</type>", "<type>i4</type>").Replace("@width", width.ToString()).Replace("@align", textalign.ToString().ToLower());
                case FieldType.DateTime:
                    return XmlPattern.Replace("@caption", field.Name).Replace("@property", olItemPropertyName).Replace("<type>string</type>", "<type>datetime</type>").Replace("@width", width.ToString()).Replace("@align", textalign.ToString().ToLower());
                default:
                    return XmlPattern.Replace("@caption", field.Name).Replace("@property", olItemPropertyName).Replace("@width", width.ToString()).Replace("@align", textalign.ToString().ToLower());

            }

        }
        public static string CreateOlXML( FieldDefinition field, string olItemPropertyName )
        {
            return CreateOlXML( field, DefaultColumnWidth, olItemPropertyName, DefaultTextAlign );
        }
        public static string CreateOlXML( WorkItemFieldDefinition field, string olItemPropertyName )
        {
            return CreateOlXML( field, DefaultColumnWidth, olItemPropertyName, DefaultTextAlign );
        }

        public string Caption
        {
            get { return this.WIFieldDef.Name; }
        }

        public int Width
        {
            get { return this.m_width;  }
            set { this.m_width = value; }
        }
        private int m_width = DefaultColumnWidth;

        public string OlItemPropertyName
        {
            get { return this.m_olItemPropertyName;  }
            set { this.m_olItemPropertyName = value; }
        }
        private string m_olItemPropertyName = string.Empty;

        public HorizontalAlign TextAlign
        {
            get { return this.m_textAlign;  }
            set { this.m_textAlign = value; }
        }
        private HorizontalAlign m_textAlign = HorizontalAlign.Left;

        public WorkItemFieldDefinition WIFieldDef
        {
            get { return this.m_fieldDef; }
        }
        private WorkItemFieldDefinition m_fieldDef = new WorkItemFieldDefinition();

        private OlWIQueryColumn()
        { }
        public OlWIQueryColumn( FieldDefinition field, string OlPropertyName ) : this()
        {
            this.WIFieldDef.Init( field );
            this.OlItemPropertyName = OlPropertyName;
        }
        public OlWIQueryColumn( FieldDefinition field, string OlPropertyName, int width, HorizontalAlign textAlign ) : this( field, OlPropertyName )
        {
            this.Width = width;
            this.TextAlign = textAlign;
        }
        public OlWIQueryColumn( WorkItemFieldDefinition field, string OlPropertyName ) : this()
        {
            this.WIFieldDef.Init( field );
            this.OlItemPropertyName = OlPropertyName;
        }
        public OlWIQueryColumn( WorkItemFieldDefinition field, string OlPropertyName, int width, HorizontalAlign textAlign ) : this(field, OlPropertyName)
        {
            this.Width = width;
            this.TextAlign = textAlign;
        }

        public string XML
        {
            get 
            {
                return CreateOlXML( this.WIFieldDef, this.Width, this.OlItemPropertyName, this.TextAlign );
            }
        }

        public static string GetOlFieldName( string wiFieldName )
        {
            return string.Format( "@{0}", wiFieldName );
        }

    }
}
